<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Payment extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('upload');
		$this->load->library('variable');
	}

	public function index() {
		$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
		$this->load->view($this->session->userdata('formain'));
		$this->load->view('layouts/footer');
	}

	public function create_resibo(){
		$data = array(
			':p_receipt_number' => $this->input->post('receipt_number'),
			':p_receipt_date' => $this->input->post('receipt_date'),
			':p_acct_id' => $this->input->post('account_id'),
			':p_payment_type' => $this->input->post('payment_type'),
			':p_remarks' => $this->input->post('remarks'),
			':p_reference' => $this->input->post('reference'),
			':p_pay_amount' => $this->input->post('payment_account'),
			$this->input->post('selected_amt')
		);
		$this->payment_model->insertResibo($data);
	}

	public function process_payment() {
		$balance = 0;
		$tmp = $this->payment_model->get_bal($this->input->post('invoice_id'));
		foreach ($tmp as $key) {
			if ((int) $this->input->post('last_ins') == 1) {
				$balance = (float) $this->input->post('bal');
			} else {
				$balance = 0;
			}
		}
		$data2 = array(
			'invoice_balance' => $balance,
		);
		$data = array(
			':p_invoice_id'      => $this->input->post('invoice_id'),
			':p_invoice_total'   => $this->input->post('invoice_total'),
			':p_unpaid_amt'      => $this->input->post('unpaid_amt'),
			':p_pay_amount'      => $this->input->post('pay_amount'),
			':p_receipt_id'      => $this->input->post('receipt_id'),
			':p_created_by'      => $this->session->userdata('user_id'),
			':p_updated_by'      => $this->session->userdata('user_id'),
			':p_last_ins'        => (int) $this->input->post('last_ins'),
			':p_acct_id'         => $this->input->post('account_id'),
			$this->input->post('receipt_app_id'),
			$this->input->post('payment_account')
		);
		//echo json_encode($data);
		$this->payment_model->insertPayments($data);
		//$this->payment_model->invoicebalupd($this->input->post('invoice_id'), $data2);
	}
	public function add_form() {
		$this->load->view($this->session->userdata('foradd'), array(
			'receipt' => $this->payment_model->getreceiptnum('RECEIPT_NUMBER'),
			'accounts' => $this->payment_model->get_active()
		));
	}

	public function view_form($id) {
		$this->load->view($this->session->userdata('forview'), array(
			'paymentdtl' => $this->payment_model->paymentdtl($id),
		));
	}

	public function search() {
		echo json_encode($this->payment_model->search($this->input->post('acct_num'), $this->input->post('acct_name')));
	}

	public function search_payment() {
		echo json_encode($this->payment_model->search_acct($this->input->post('acct_num'), $this->input->post('acct_name')));
	}

	public function getbalance() {
		echo json_encode($this->payment_model->get_bal($this->input->post('invoice_id')));
	}

	public function getrecnum() {
		echo json_encode($this->payment_model->getreceiptnum('RECEIPT_NUMBER'));
	}

	public function getReceipt() {
		echo json_encode($this->payment_model->getrec($this->input->post('receipt_number')));
	}

	public function getReceipt2() {
		echo json_encode($this->payment_model->getrec2($this->input->post('receipt_number')));
	}

	public function genAdvance(){
		echo json_encode($this->payment_model->genAdv($this->input->post('receipt_number')));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */